/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var teamUserClass = require('../class/teamuser');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var teamUser = new teamUserClass();

        var checkResult = teamUser.checkParams(
            [
                'teamId',
                'userId',
                'level',
                'createUid'
            ],
            params);
        if (checkResult) {
            cb( util.getErrorRes('400', checkResult),null);
            return;
        } else {
            teamUser.init(params);
        }

        models.teamuser.findOrCreate({teamId:teamUser.getParam('teamId'),userId:teamUser.getParam('userId')},teamUser.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var teamUser = new teamUserClass();

        var checkResult = teamUser.checkParams(['rowId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult),null);
            return;
        } else {
            teamUser.init(params);
        }

        models.teamuser.findOne(teamUser.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    getList:function(params,cb){

        var models=util.models;

        if(!models){
            var err=new Error();
            err.status=500;
            err.message="数据库连接失败";
            cb(err,null);
            return;
        }

        models.teamuser.find().exec(function (err, data){
            if (err) {
                cb(err, null);
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    getListByUId:function(params,cb){

        var models=util.models;

        if(!models){
            var err=new Error();
            err.status=500;
            err.message="数据库连接失败";
            cb(err,null);
            return;
        }

        var teamUser = new teamUserClass();

        var checkResult = teamUser.checkParams(['userId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult),null);
            return;
        } else {
            teamUser.init(params);
        }
        models.teamuser.find(teamUser.getParams()).exec(function (err, data){
            if (err) {
                cb(err, null);
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    getListByTeamId:function(params,cb){

        var models=util.models;

        if(!models){
            var err=new Error();
            err.status=500;
            err.message="数据库连接失败";
            cb(err,null);
            return;
        }

        var teamUser = new teamUserClass();

        var checkResult = teamUser.checkParams(['teamId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult),null);
            return;
        } else {
            teamUser.init(params);
        }
        models.teamuser.find(teamUser.getParams()).exec(function (err, data){
            if (err) {
                cb(err, null);
            }
            cb(null, util.getSuccessRes(data));
        });
    }



};



